<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="排序" prop="sort">
				<el-input-number v-model="form.sortNo" :min="1"></el-input-number>
			</el-form-item>
			<el-form-item label="模版名称" prop="title">
				<el-input v-model="form.templateName" placeholder="模版名称" clearable></el-input>
			</el-form-item>
			<el-col :lg="12">
				<el-form-item label="公告类型" prop="noticeType">
					<sc-select v-model="form.noticeType" :params="{code:'noticeType'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
				</el-form-item>
			</el-col>
			<el-form-item label="是否有效">
				<el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
			<el-form-item label="模版文件" prop="attachId">
				<sc-upload-file :apiObj="apiObj" v-model="fileList" :limit="1" :data="{module:'notice'}" :showFileList="false" @success="addNewFile"
								tip="只能上传单个文件不要超过10M,请上传Html格式文件"
								accept="text/html">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" maxlength="150" clearable type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
import AttachMenu from '@/views/module/attachMenu'
export default {
	components: {
		AttachMenu
	},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增',
				edit: '编辑',
				show: '查看'
			},
			apiObj:this.$API.file.attach.uploadFile,
			visible: false,
			isSaveing: false,
			dbOptions:[],
			fileList:[],
			//表单数据
			form: {
				templateId:'',
				sortNo: 1,
				templateName:"",
				noticeType:"",
				attachId:"",
				status: "1",
				remark: ""
			},
			//验证规则
			rules: {
				templateName: [
					{required: true, message: '请输入模版名称'}
				],
				attachId:[
					{required: true, message: '请上传模版文件'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode='add'){
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit(){
			if(this.mode=="add")
			{
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = await this.$API.info.notice.insertNoticeTemplate.post(this.form);
						this.isSaveing = false;
						if(res.code == 200){
							this.$emit('success')
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			}else if(this.mode=="edit")
			{
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = await this.$API.info.notice.updateNoticeTemplate.post(this.form);
						this.isSaveing = false;
						if(res.code == 200){
							this.visible = false;
							this.$emit('success')
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			}
		},
		setData(data){
			this.form.templateId = data.templateId
			this.form.templateName = data.templateName
			this.form.sortNo = data.sortNo
			this.form.attachId = data.attachId
			this.form.noticeType = data.noticeType
			this.form.status = data.status
			this.form.remark = data.remark
		},
		addNewFile(res, file)
		{
			this.form.attachId = res.data.attachId;
		}
	}
}
</script>

<style>
</style>
